/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include <fstream>

#include "Parser.h"
#include "StringUtil.h"
#include "MFile.h"
//#include "../Kernel.h"
//
//extern Kernel* g_kernel;

#define NEW_LINE 10

Parser::Parser(MFile* file, int end)
{
    m_readEnd = end;
    m_strList = ParseFile( file );
    Print();
}

/** Get next valid string */
std::string Parser::GetNextLine()
{
    std::string str = m_strList.front();
    m_strList.pop_front();
    return str;
}

/** Read through the file */
type_StringList Parser::ParseFile(MFile* file)
{
    char c;
    std::string line;
    bool inComment = false;
    type_StringList list;

    while( !file->IsEnd() && file->GetPosition() < m_readEnd )
    {
        c = file->Read();
        if ( inComment )
        {   // eat everything until we reach the end comment
            while( !file->IsEnd() && file->GetPosition() < m_readEnd && inComment ) {
                if ( c == '*' ) {
                    if ( file->Peek() == '/' )
                    {
                        file->Read();
                        inComment = false;
                    }
                }
                c = file->Read();
            }
        }
        // single line comments
        if ( c == '%' && !inComment )
        {
            // read until newline or eof
            while( !file->IsEnd() && file->GetPosition() < m_readEnd && c != NEW_LINE ) {
                c = file->Read();
            }
        }
        // multiple line comments
        if ( c == '/' && !inComment )
        {
            if ( file->Peek() == '*' ) {
                inComment = true;
                file->Read(); continue;
            }
        }

        line += c;
        if ( c == NEW_LINE ) { list.push_back( line ); line.clear(); }
    }
    if ( !line.empty() ) list.push_back( line );
    return list;
}

/** Test out the parser */
void Parser::Print()
{
    type_StringList::iterator it = m_strList.begin();
    for(; it != m_strList.end(); ++it )
    {
        //g_kernel->LPrintf( ((*it)).c_str() );
    }
}

Parser::~Parser(void)
{
}
